import { request } from "../../utils/request.js";
import regeneratorRuntime from "../../utils/runtime.js";
var bsurl = require('../../utils/bsurl.js');
var app=getApp();
Page({
  data: {
    playlistInfo:{},
    playListId:0,
    aid:0,
    authorimg:'',
    author:'',
    privileges:[],
    id:0,
    palyingId:''
  },
  //获取歌单信息
  async getplaylistItem(id){
    const result=await request({url: bsurl+'playlist/detail?id='+id});
    let {coverImgUrl,name}=result.playlist;
    const playlistInfo={coverImgUrl,name};
    const privileges=result.playlist.tracks.map(v=>{
      return {
        id:v.id,
        name:v.name,
        author:v.ar[0].name
      }
    });
    this.setData({
      playlistInfo,
      privileges,
      authorimg:result.playlist.creator.avatarUrl,
      author:result.playlist.creator.nickname
    })
  },
  //专辑
  async getAlbum(aid){
    const result=await request({url: bsurl+'album?id='+aid});
    let name=result.album.name;
    let coverImgUrl=result.album.picUrl;
    const playlistInfo={coverImgUrl,name};
    const privileges=result.songs.map(v=>{
      return {
        id:v.id,
        name:v.name,
        author:v.ar[0].name
      }
    });
    this.setData({
      playlistInfo,
      privileges,
      authorimg:result.album.artist.picUrl,
      author:result.album.artist.name
    })
  },
   handletap(e){
    const {index}=e.currentTarget.dataset;
    this.setData({
      id:index
    })
  },
  onLoad: function(options) {
    const id=options.id;
    const aid=options.aid;
    if(id){
      this.getplaylistItem(id);
      this.setData({
        playListId:id,
      })
    }else{
      this.getAlbum(aid)
      this.setData({
        aid
      })
    }
    this.setData({
      id:app.globalData.playingId
    })
  },
  onReady: function() {
    
  },
  onShow: function() {
     let that=this;
    getApp().watchplayid(that.watchBackPalyid) 
  },
  //监听播放id变化
  watchBackPalyid: function (value){
    //console.log(value);
    this.setData({
      id:value
    })
  },
});
  
  